package org.bing.engine.utility.helper;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class HostHelper {

    public static String getHostAddress() {
        String[] ips = getAllHostAddress();
        for (String ip : ips) {
            if (ip != null && !ip.startsWith("127.") && !ip.startsWith("localhost")) {
                return ip;
            }
        }
        return "0.0.0.0";
    }

    public static String getHostName() {
        String hostName;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            hostName = addr.getHostName();
        } catch (Exception ex) {
            hostName = "unknown";
        }
        return hostName;
    }

    public static String[] getAllHostAddress() {
        List<String> ips = new ArrayList<String>();
        Enumeration<NetworkInterface> nie = null;
        try {
            nie = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return new String[0];
        }
        while (nie.hasMoreElements()) {
            NetworkInterface ni = nie.nextElement();
            Enumeration<InetAddress> addrs = ni.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = addrs.nextElement();
                if (!addr.isLoopbackAddress() && addr.getHostAddress().indexOf(":") == -1) {
                    String ip = addr.getHostAddress();
                    ips.add(ip);
                }
            }
        }
        return ips.toArray(new String[ips.size()]);
    }

    public static String[] getHostAddressByHostName(String hostName) {
        String[] result = null;
        try {
            if (hostName.length() > 0) {
                InetAddress[] addrs = InetAddress.getAllByName(hostName);
                if (addrs.length > 0) {
                    result = new String[addrs.length];
                    for (int i = 0; i < addrs.length; i++) {
                        result[i] = addrs[i].getHostAddress();
                    }
                }
            }

        } catch (Exception ex) {
            result = new String[] {};
        }
        return result;
    }

    public static String[] getAllHostMacAddress() {
        List<String> ips = new ArrayList<String>();
        Enumeration<NetworkInterface> nie = null;
        try {
            nie = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return new String[0];
        }
        while (nie.hasMoreElements()) {
            NetworkInterface ni = nie.nextElement(); 
            try {
                byte[] bs = ni.getHardwareAddress();
                if (bs != null) {
                    StringBuilder sb = new StringBuilder();
                    for (byte b : bs) {
                        sb.append(hexByte(b));
                        sb.append("-");
                    }
                    System.out.println(ni.getName() + " ==> " + sb);
                }
            } catch (SocketException e) {
                e.printStackTrace();
                // ignore
            }
        }
        return ips.toArray(new String[ips.size()]);
    }

    public static String getHostMacAddressByIp(String ip) {
        return null;
    }

    private static String hexByte(byte b) {
        String s = "000000" + Integer.toHexString(b);
        return s.substring(s.length() - 2);
    }

    public static void main(String[] args) throws SocketException {
        System.out.println("HostName:" + HostHelper.getHostName());
        System.out.println("HostAddr:" + HostHelper.getHostAddress());
        String[] allAddress = HostHelper.getAllHostAddress();
        for (int i = 0; i < allAddress.length; i++) {
            System.out.println(i + "> " + allAddress[i]);
        }
        HostHelper.getAllHostMacAddress();
    }
}
